<template>
    <div class="login_container">
      <div class="text">
        <h3>信大学生宿舍管理系统</h3>
      </div>
      <div class="login_box">
        <!--头像区域-->
        <div class="avatar_box">
          <img src="../assets/images/logo.png"/>
        </div>
        <!--登录表单区域-->
        <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" class="login_form">
          <!--用户名-->
          <el-form-item prop="loginName">
            <el-input v-model="loginForm.loginName" prefix-icon="iconfont icon-icon_login_input_user "></el-input>
          </el-form-item>
          <!--密码-->
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" prefix-icon="iconfont icon-lock_fill" type="password"></el-input>
          </el-form-item>
          <!--单选按钮区域-->
          <el-form-item class="form_radio" >
            <el-radio-group v-model="radio" @change="selectRadio">
                <el-radio v-for="(item, index) in items" :label="index">{{item.value}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <!--按钮区域-->
          <el-form-item class="btns">
            <el-button type="primary" @click="login">登录</el-button>
            <el-button type="info" @click="resetLoginForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
</template>

<script>
    export default {
      name: "Login",
      data(){
          return{
            //这是登录表单的数据绑定对象
            loginForm: {
              loginName: '',
              password: ''
            },
            //表单验证规则
            loginFormRules: {
              loginName:[
                { required: true, message: '请输入账号',trigger: 'blur'}
              ],
              password:[
                { required: true, message: '请输入密码', trigger: 'blur'},
                { min: 6, max: 16, message: '长度在6-16个字符', trigger: 'blur'}
              ]
            },
            //单选按钮
            radio: 0,
            items: [
              { value: '系统管理员', prams: 1},
              { value: '宿舍管理员', prams: 2},
              { value: '学生', prams: 3}
            ]
          }
      },
	  
	  //http://47.101.150.115/DMS/adminManageController/userLogin
	  
      methods:{
        //登录预验证
        login(){
          this.$refs.loginFormRef.validate(async valid => {
            if(!valid) return;
            // 请求登录
            const res = await this.$axios.post(this.$http + '/adminManageController/userLogin',{
                userName: this.loginForm.loginName,
                password: this.$md5(this.loginForm.password),
                userType: this.radio
            }).catch(err => {
              this.$message.error(err)
            });
            console.log(res);
            //登录成功后，将用户信息保存在cookie中
            this.$cookies.set('user', res.data.datas.user, 0);
            if (res.data.code === 200)
               this.$message.success("登录成功");
            else if (res.data.code === 100)
              return this.$message.error("登录失败");
            else if (res.data.code === 300)
              return this.$message.warning("账号状态已被关闭，请联系系统管理员");
            // 1、登录成功 获取token保存到客户端的sessionstorage中
            window.sessionStorage.setItem("token",res.data.datas.token);
            // 2、通过编程式导航跳转到后台首页
            if (this.radio === 0) this.$router.push('home');
            else if (this.radio === 1) this.$router.push('home');
            else if (this.radio === 2) this.$router.push('home');
          });
        },

        /*单选按钮*/
        selectRadio(){
          console.log(this.radio)
        },

        //重置登录表单
        resetLoginForm(){
          this.$refs.loginFormRef.resetFields();
        },
      }
    }
</script>

<style lang="less" scoped>
  .login_container{
    background-color: #2b4b6b;
    height:100%;
  }
  .text {
    text-align: center;
    h3{
      color: #fff;
      font-size: 27px;
    }
  }
  //登录盒子居中
  .login_box{
    width: 450px;
    height: 300px;
    border-radius: 3px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .avatar_box{
      height: 110px;
      width: 110px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd; //阴影
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #fff;
      img{
        width: 100%;
        height: 100%;
        border-radius: 50%;
        border: 1px solid #eee;
      }
    }
  }

  //表单区域定位
  .login_form{
    position: absolute;
    bottom: 0;
    top: 80px;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
  .form_radio{
    margin-bottom: 10px;
    margin-top: -15px;
  }
  //按钮
  .btns{
    display: flex;
    justify-content: flex-end;
  }

</style>
